package com.afkt.project.ui.slice;

import com.afkt.project.model.item.ButtonList;
import com.afkt.project.model.item.ButtonValue;
import com.afkt.project.ui.adapter.ButtonAdapter;
import com.afkt.project.util.SkipUtils;
import com.dev.base.DevObject;
import com.dev.base.ability.MVPAbilitySlice;
import com.dev.base.ability.MVVMAbilitySlice;
import com.dev.other.EventBusUtils;
import org.greenrobot.eventbus.Subscribe;
import com.dev.utils.app.toast.ToastTintUtils;
import com.ohos.afkt.ResourceTable;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import org.greenrobot.eventbus.ThreadMode;

public class EventBusAbility extends AbilitySlice {
    private DirectionalLayout bar_layout;

    private Image ui_return;

    private DirectionalLayout ui_bar;

    private ListContainer UI_list;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_framework);
        bar_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_bar_layout);
        ui_bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_ui_bar, null, false);
        Text text = (Text) ui_bar.findComponentById(ResourceTable.Id_title_name);
        text.setText("EventBus");
        bar_layout.addComponent(ui_bar);
        UI_list = (ListContainer) findComponentById(ResourceTable.Id_UI_List);
        ui_return = (Image) ui_bar.findComponentById(ResourceTable.Id_ui_return);
        ui_return.setClickedListener(component -> onBackPressed());
        ButtonAdapter buttonAdapter = new ButtonAdapter(ButtonList.getEventButtonValues(), this);
        UI_list.setItemProvider(buttonAdapter);
        UI_list.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                ButtonValue buttonValue = (ButtonValue) UI_list.getItemProvider().getItem(i);
                switch (buttonValue.type) {
                    case ButtonValue.BTN_EVENT_REGISTER:
                        ToastTintUtils.info(true, "注册成功");
                        EventBusUtils.register(EventBusAbility.this);
                        break;
                    case ButtonValue.BTN_EVENT_UNREGISTER:
                        ToastTintUtils.info(true, "解绑成功");
                        EventBusUtils.unregister(EventBusAbility.this);
                        break;
                    case ButtonValue.BTN_EVENT_CLEAN_STICKY:
                        ToastTintUtils.info(true, "清空全部粘性事件成功");
                        EventBusUtils.removeAllStickyEvents();
                        break;
                    case ButtonValue.BTN_EVENT_SEND:
                        ToastTintUtils.info(true, "发送事件成功");
                        DevObject<String> event = new DevObject<>();
                        event.setCode(1).setObject("正常消息");
                        EventBusUtils.post(event);
                        break;
                    case ButtonValue.BTN_EVENT_SEND_STICKY:
                        ToastTintUtils.info(true, "发送粘性事件成功");
                        DevObject<String> eventSticky = new DevObject<>();
                        eventSticky.setCode(2).setObject("粘性消息");
                        EventBusUtils.postSticky(eventSticky);
                        // 如何测试粘性消息, 先注册并发送粘性事件, 然后解绑, 再次注册(则会再次接收到粘性事件消息)
                        break;
                    default:
                        ToastTintUtils.warning("未处理 " + buttonValue.text + " 事件");
                        break;
                }
            }
        });
    }

    /**
     * 哪怕不用这个方法，必须写一个，不然报错
     * org.greenrobot.eventbus.EventBusException:
     * Subscriber class com.afkt.project.ui.slice.EventBusAbility
     * and its super classes have no public methods with the @Subscribe annotation
     * @param myEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Object myEvent) {

    }
    @Override
    public void onActive() {
        super.onActive();
        if (bar_layout.getChildCount() == 0) {
            bar_layout.addComponent(ui_bar);
        }
    }

    @Override
    protected void onBackground() {
        super.onBackground();
        EventBusUtils.removeAllStickyEvents();
        EventBusUtils.unregister(EventBusAbility.this);
        bar_layout.removeComponent(ui_bar);
    }
}
